/*
 * This file is part of quickReminder, a software for gamers
 *
 * Copyright (C) 2006-2008 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.Text;
using ESLReminder;
using System.Text.RegularExpressions;

namespace LucasCode
{
    public class ConsoleParserHL2 : ConsoleParserBase
    {
        private List<MatchPlayer> m_Players = new List<MatchPlayer>();

        public MatchPlayer[] Players
        {
            get
            {
                return m_Players.ToArray();
            }
        }

        private MatchPlayer GetPlayer(List<MatchPlayer> players, string name)
        {
            foreach (MatchPlayer p in players)
            {
                if (p.CurrentNick == name)
                {
                    return p;
                }
            }
            MatchPlayer pl = new MatchPlayer(name);
            players.Add(pl);
            return pl;
        }

        private string FromHL2String(string input)
        {
            UTF8Encoding u = new UTF8Encoding();
            //Encoding e = Encoding.GetEncoding(1252);
            Encoding e = Encoding.GetEncoding(1252);
            return u.GetString(e.GetBytes(input));
        }

        public override void Parse(ESLReminder.ConsoleEventItemCollection ConsoleEventList)
        {
            foreach (ConsoleEventItem ev in ConsoleEventList)
            {
                Match m = Regex.Match(ev.EventText, @"^(.*?) killed (.*?) with (.*?)\.$");
                if (m.Success)
                {
                    MatchPlayer Killer = GetPlayer(m_Players, FromHL2String(m.Groups[1].Value));
                    MatchPlayer Killee = GetPlayer(m_Players, FromHL2String(m.Groups[2].Value));
                    Killer.AddAction(new MatchAction(MatchActions.Kills, Killer, Killee));
                    Killee.AddAction(new MatchAction(MatchActions.Dies, Killee, Killer));
                }
            }
        }
    }
}
